package me.firstapp.weixin.controller.soft;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.member.Member;
import me.firstapp.module.weixin.member.SoftFee;
import me.firstapp.module.weixin.order.SoftOrder;
import me.firstapp.service.weixin.member.MemberService;
import me.firstapp.service.weixin.soft.SoftFeeService;
import me.firstapp.service.weixin.soft.SoftOrderService;
import me.firstapp.weixin.controller.base.BaseController;

@Controller
public class SoftController extends BaseController {

	@Autowired
	private SoftOrderService softOrderService;

	@Autowired
	private MemberService memberService;

	@Autowired
	private SoftFeeService softFeeService;

	@RequestMapping("/page/softBuyPage.htm")
	public ModelAndView softBuyPage(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String openId = getCurrentLoginOpenId(request, response);
		Member member = memberService.findMemberByOpenId(openId);
		if (null == member) {
			throw new ServiceException(StatusHouse.STATUS_PAGE_ERROR_NOT_CONCERN_WEIXIN);
		}
		modelMap.put("avatar", member.getAvatar());
		modelMap.put("name", member.getName());
		modelMap.put("level", member.getVipLevel());
		modelMap.put("id", member.getCardNo());

		SoftFee baseVipSoftFee = softFeeService.findSoftFeeByLevel(Member.VIP_LEVEL_BASE);
		SoftFee heighVipSoftFee = softFeeService.findSoftFeeByLevel(Member.VIP_LEVEL_HIGHT);
		modelMap.put("baseVipSoftFee", baseVipSoftFee);
		modelMap.put("heighVipSoftFee", heighVipSoftFee);
		return new ModelAndView("soft/softBuy", modelMap);
	}

	@RequestMapping("/page/confirmSoftOrder.do")
	public void confirmSoftOrder(Integer level, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<SoftOrder> result = new SingleObject<>();
		try {
			if (null == level) {
				throw new ServiceException(StatusHouse.COMMON_PARAMETER_ERROR);
			}
			if (Member.VIP_LEVEL_BASE != level && Member.VIP_LEVEL_HIGHT != level) {
				throw new ServiceException(StatusHouse.COMMON_PARAMETER_ERROR);
			}
			String openId = getCurrentLoginOpenId(request, response);
			Member member = memberService.findMemberByOpenId(openId);
			if (null == member) {
				throw new ServiceException(StatusHouse.STATUS_PAGE_ERROR_NOT_CONCERN_WEIXIN);
			}
			SoftOrder softOrder = softOrderService.executeCreateSoftOrder(openId, level);
			result.setObject(softOrder);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping("/page/confirmSoftOrderPage.htm")
	public ModelAndView confirmSoftOrderPage(String orderNo, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		String openId = getCurrentLoginOpenId(request, response);
		Member member = memberService.findMemberByOpenId(openId);
		if (null == member) {
			throw new ServiceException(StatusHouse.STATUS_PAGE_ERROR_NOT_CONCERN_WEIXIN);
		}
		if (StrUtils.isNULL(orderNo)) {
			throw new ServiceException(StatusHouse.COMMON_PARAMETER_ERROR);
		}
		SoftOrder softOrder = softOrderService.findByOrderNo(orderNo);
		if (null == softOrder) {
			throw new ServiceException(StatusHouse.STATUS_ORDER_NOT_EXIST);
		}
		modelMap.put("softOrder", softOrder);
		return new ModelAndView("soft/confirmSoftOrder", modelMap);
	}

}
